package com.fasteat.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasteat.mall.entity.VisitedHistory;
import com.fasteat.mall.entity.vo.ProductVisitedHistoryVo;
import com.fasteat.mall.mapper.VisitedHistoryMapper;
import com.fasteat.mall.service.VisitedHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VisitedHistoryServiceImpl extends ServiceImpl<VisitedHistoryMapper, VisitedHistory> implements VisitedHistoryService {

	@Autowired
	private VisitedHistoryMapper visitedHistoryMapper;

	@Override
	public List<ProductVisitedHistoryVo> getAllProductVisitedHistoriesByUserId(String userId) {
		return visitedHistoryMapper.findAllProductVisitedHistoriesByUserId(userId);
	}

	@Override
	public boolean removeAllAllProductVisitedHistoriesByUserId(String userId) {
		return visitedHistoryMapper.deleteAllProductVisitedHistoriesByUserId(userId) > 0;
	}

	@Override
	public Page<ProductVisitedHistoryVo> getAllProductVisitedHistoriesByUserIdWithPage(String userId, int current, int size) {
		if (current <= 0) {
			current = 1;
		}
		if (size <= 0) {
			size = 5;
		}
		Page<ProductVisitedHistoryVo> page = new Page<>(current, size);
		List<ProductVisitedHistoryVo> records = visitedHistoryMapper.findAllProductVisitedHistoriesByUserIdWithPage(userId, (current - 1) * size, size);
		int total = visitedHistoryMapper.findCountByUserId(userId);
		page.setRecords(records);
		page.setTotal(total);
		page.setPages(total / size + total % size == 0 ? 0 : 1);
		return page;
	}
}
